package com.easy;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lxk on 2019/5/26.
 */
public class Solution202 {

    private Integer getNum(Integer num){
        Integer sum = 0;
        Integer curVal = 0;
        while(num!=0){
            curVal = num%10;
            sum += (curVal*curVal);
            num /= 10;
        }
        return sum;
    }


    private boolean isContain(Integer num,List<Integer> nums){
        for(Integer val : nums){
            if(val.compareTo(num) == 0){
                return true;
            }
        }
        return false;
    }

    public boolean isHappy(int n) {
        List<Integer> midRes = new ArrayList<>();
        while(!isContain(n,midRes)){
            midRes.add(n);
            n = getNum(n);
            if(n==1){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Solution202 solution202 = new Solution202();
        System.out.println(solution202.isHappy(19));
    }

}
